<template>
  <div class="list-table">
    <div class="nospecialModel ">
      <div class="area-header">
        <span class="area-header-title">{{ $t('主要人员') }}</span>
      </div>
      <el-form ref="queryForm" :model="queryParams" :inline="true">
        <el-form-item :label="$t('人员名称')" prop="staffName">
          <el-input
            v-model="queryParams.staffName"
            :placeholder="$t('人员名称')"
            clearable
            @keyup.enter.native="queryHandle"
          />
        </el-form-item>
        <el-form-item :label="$t('类型')" prop="staffType">
          <el-select v-model="queryParams.staffType" value="" :placeholder="$t('类型')" clearable>
            <el-option
              v-for="dict in staffTypeOptions"
              :key="dict.dictValue"
              :label="$t(dict.dictLabel)"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t("搜索") }}</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
        </el-form-item>
      </el-form>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            v-hasPermi="['company:staff:edit']"
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="addHandle"
          >{{ $t("新增") }}</el-button>
        </el-col>
      </el-row>
    </div>
    <div class="specialModel">
      <!-- 不可删除 -->
      <div class="area-header left">
        <span class="area-header-title">{{ $t('主要人员') }}</span>
      </div>
      <div v-if="sourceType !== 'bussRisk'" class="right">
        <el-button
          v-hasPermi="['company:staff:edit']"
          icon="el-icon-plus"
          size="mini"
          type="primary"
          @click="addHandle"
        >{{ $t("新增") }}</el-button>
      </div>
    </div>

    <el-table v-loading="loading" tooltip-effect="darkTable" :data="companyStaffList" :height="height" border>
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('人员名称')" align="center" prop="staffName" show-overflow-tooltip>
        <template slot-scope="scope">
          <div style="display: flex;align-items: center;">
            <img v-if="scope.row.logoInfo" :src="scope.row.logoInfo" width="35" height="35" style="border-radius: 50%; ">
            <el-link v-if="sourceType !== 'bussRisk'" type="primary" class="link_a" style="padding-left:5px;" @click="updateHandle(scope.row)">{{ scope.row.staffName }}</el-link>
            <el-link v-else-if="sourceType === 'bussRisk' && scope.row.tycId && scope.row.companyTycId" type="primary" class="link_a" style="padding-left:5px;" @click="getCompanyStaffInfo(scope.row)">{{ scope.row.staffName }}</el-link>
            <span v-else>{{ scope.row.staffName }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column :label="$t('类型')" align="center" prop="staffType" :formatter="staffTypeFormat" show-overflow-tooltip />
      <el-table-column :label="$t('职位')" align="center" prop="postType" show-overflow-tooltip />
      <el-table-column v-if="sourceType !== 'bussRisk'" :label="$t('联系电话')" align="center" prop="phone" show-overflow-tooltip />
      <el-table-column v-if="sourceType !== 'bussRisk'" :label="$t('邮箱地址')" align="center" prop="email" show-overflow-tooltip />
      <el-table-column v-if="sourceType !== 'bussRisk'" :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="100">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['company:staff:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改主要人员对话框 -->
    <el-dialog :title="$t(title)" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <companyStaffForm ref="companyStaffForm" />
    </el-dialog>
  </div>
</template>

<script>
import { listCompanyStaff, delCompanyStaff, exportCompanyStaff } from '@/api/company/staff/companyStaff'
import companyStaffForm from './companyStaffForm.vue'
export default {
  components: {
    companyStaffForm
  },
  props: {
    sourceType: {
      type: String,
      default: ''
    },
    height: {
      type: String,
      default: () => ''
    }
  },
  data() {
      return {
          // 遮罩层
          loading: true,
          // 总条数
          total: 0,
          // 主要人员表格数据
          companyStaffList: [],
          // 弹出层标题
          title: '主要人员',
          // 是否显示弹出层
          formOpen: false,
          // 查询参数
          queryParams: {
              companyId: undefined,
              tycId: undefined,
              staffName: undefined,
              staffType: undefined,
              pageNum: 1,
              pageSize: 10
          },
        // 类型
        staffTypeOptions: [],
          // 表单参数
          form: {},
          // 表单校验
          rules: {
          },
          // 类型字典
          staffTypeOption: []
      }
  },
  created() {
    this.getDicts('company_person').then(response => {
      this.staffTypeOptions = response.data
    })
    this.getList()
  },
  methods: {
    /** 查询主要人员列表 */
    getList() {
        this.loading = true
        this.queryParams.companyId = this.$route.query.companyId
        listCompanyStaff(this.queryParams).then(response => {
            this.companyStaffList = response.rows
            this.total = response.total
            this.loading = false
        })
    },
    // 类型字典翻译
    staffTypeFormat(row) {
      return this.selectDictLabel(this.staffTypeOptions, row.staffType)
    },

    // 表单重置
    reset() {
        this.form = {
            id: undefined,
            companyId: undefined,
            tycId: undefined,
            companyTycId: undefined,
            staffName: undefined,
            logoInfo: undefined,
            staffType: undefined,
            postType: undefined,
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined,
            remark: undefined,
            delFlag: undefined
        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.resetForm('queryForm')
        this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
        this.formOpen = true
        this.$nextTick(() => {
            this.$refs.companyStaffForm.init(null)
        })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
        this.formOpen = true
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.companyStaffForm.init(row)
        })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return delCompanyStaff(row.id)
        }).then(() => {
            this.getList()
            this.msgSuccess(this.$t('删除成功'))
        }).catch(function() {
        })
    },
    /** 导出按钮操作 */
    exportHandle() {
        const queryParams = this.queryParams
        this.$confirm(this.$t('是否确认导出?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return exportCompanyStaff(queryParams)
        }).then(response => {
            this.download(response.msg)
        }).catch(function() {
        })
    },
    getCompanyStaffInfo(row) {
      const staffTycId = row.tycId
      const companyTycId = row.companyTycId
      window.open('https://www.tianyancha.com/human/' + staffTycId + '-c' + companyTycId, '_blank')
    }
  }
}
</script>
